package com.ezjie.ielts.module_word.adapter;

import java.util.List;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.ezjie.ielts.R;
import com.ezjie.ielts.db.WordDao;
import com.ezjie.ielts.db.WordInstanceDao;
import com.ezjie.ielts.db.WordMeaningDao;
import com.ezjie.ielts.db.WordQuestionDao;
import com.ezjie.ielts.model.Eword;
import com.ezjie.ielts.model.EwordInstance;
import com.ezjie.ielts.model.EwordMeaning;
import com.ezjie.ielts.model.Mean;
import com.ezjie.ielts.model.WordBean;
import com.ezjie.ielts.util.ListUtils;

public class ReviewFinishAdapter extends BaseExpandableListAdapter {

	private Context mContext;
	private List<WordBean> mList;
	private WordDao dao;
	private WordInstanceDao instanceDao;
	private WordMeaningDao meaningDao;
	private WordQuestionDao questionDao;

	public ReviewFinishAdapter(Context mContext) {
		super();
		this.mContext = mContext;
		dao = new WordDao(mContext);
		instanceDao = new WordInstanceDao(mContext);
		meaningDao = new WordMeaningDao(mContext);
		questionDao = new WordQuestionDao(mContext);
	}

	public List<WordBean> getmList() {
		return mList;
	}

	public void setmList(List<WordBean> mList) {
		this.mList = mList;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mList == null ? 0 : mList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mList.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return mList.get(groupPosition).getMeans().get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (view == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.review_finish_item, null);
			holder = new ViewHolder();
			holder.mean = (TextView) view.findViewById(R.id.mean);
			holder.soundmark = (TextView) view.findViewById(R.id.soundmark);
			holder.word = (TextView) view.findViewById(R.id.word);
			holder.item_header_line = view.findViewById(R.id.item_header_line);
			holder.view_fooder_line = view.findViewById(R.id.view_fooder_line);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		if (isExpanded) {
			holder.mean.setVisibility(View.GONE);
			holder.view_fooder_line.setVisibility(View.GONE);
		} else {
			holder.mean.setVisibility(View.VISIBLE);
			holder.view_fooder_line.setVisibility(View.VISIBLE);
		}

		if (groupPosition == 0) {
			holder.item_header_line.setVisibility(View.GONE);
		} else {
			holder.item_header_line.setVisibility(View.VISIBLE);
		}
		WordBean mBean = mList.get(groupPosition);
		if (mBean != null) {
			if (TextUtils.isEmpty(mBean.getWord())) {
				Eword eword = dao.findByWordId(mBean.getWid());
				List<EwordMeaning> wordmMeanings = meaningDao.findByWordId(mBean.getWid());
				// List<EwordQuestion> wordQuestions =
				// questionDao.findByWordId(mBean.getWid());
				List<EwordInstance> wordInstances = instanceDao.findByWordId(mBean.getWid());
				if (eword != null) {// && wordQuestions != null
					mBean.setWord(eword.word);
					mBean.setPhonetic(eword.en_phonetic);
					mBean.setMeans(ListUtils.transferMeanList(wordmMeanings));
					mBean.setInstances(ListUtils.transferInstanceList(wordInstances));
					// mWordBean.get(i).setQuestion_type(ListUtils.transferQuestionType(wordQuestions));
					// mWordBean.get(i).setQuestions(ListUtils.transferQuestionList(wordQuestions));
				}
			}
			List<Mean> means = mBean.getMeans();
			if (means != null && means.size() > 0) {
				holder.mean.setText(Html.fromHtml(means.get(0).getPos() + means.get(0).getText()));
				if (!TextUtils.isEmpty(mBean.getPhonetic())) {
					holder.soundmark.setText(Html.fromHtml(mBean.getPhonetic()));
				}
				if (TextUtils.isEmpty(mBean.getWord())) {
					holder.word.setText("服务器返回空了");
				} else {
					holder.word.setText(Html.fromHtml(mBean.getWord()));
				}
			}
		}
		return view;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		// TODO Auto-generated method stub
		ChildViewHolder mHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.review_summary_child_item, null);
			mHolder = new ChildViewHolder();
			mHolder.means = (TextView) convertView.findViewById(R.id.tv_means);
			mHolder.soundmark = (TextView) convertView.findViewById(R.id.soundmark);
			convertView.setTag(mHolder);
		} else {
			mHolder = (ChildViewHolder) convertView.getTag();
		}

		WordBean mBean = mList.get(groupPosition);
		if (mBean != null) {
			List<Mean> means = mBean.getMeans();
			if (means != null) {
				mHolder.means.setText(Html.fromHtml(ListUtils.getTransferStrByList(means)));
				mHolder.soundmark.setText(Html.fromHtml(mBean.getPhonetic()));
			}
		}
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}

	class ViewHolder {
		TextView word, soundmark, mean;
		View item_header_line, view_fooder_line;
	}

	class ChildViewHolder {
		TextView means, soundmark;
	}
}
